<template>
  <div class="app-container">
    <el-row :gutter="24">
      <!--部门数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="节目标题" prop="courseTitle">
            <el-input
              v-model="queryParams.courseTitle"
              placeholder="请输入电台节目名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="节目状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择节目状态" clearable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <router-link :to="{path:'/maintaincourse/add',meta:{ title: 'ss'}}" class="link-type">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['course:maintaincourse:add']"
              >新增</el-button>
            </router-link>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['course:maintaincourse:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['course:maintaincourse:remove']"
            >删除</el-button>
          </el-col>
        </el-row>

        <el-table
          v-loading="loading"
          :data="maintaincourseList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="节目编号" align="center" width="80"  prop="courseId" />
          <el-table-column label="节目名称" align="center" prop="courseTitle" />
          <!-- <el-table-column label="课程类型" align="center" prop="courseTypeId" /> -->
          <!-- <el-table-column label="浏览人数" align="center" prop="viewNum" /> -->
          <!-- <el-table-column label="点赞人数" align="center" prop="likeNum" /> -->
          <!-- <el-table-column label="课程缩略图路径" align="center" prop="courseThumbUrl" /> -->
          <!-- <el-table-column label="课程起始开始时间" align="center" prop="validStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程起始结束时间" align="center" prop="validEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validEndTime) }}</span>
        </template>
          </el-table-column>-->
          <!-- <el-table-column label="课程等级" align="center" prop="level" :formatter="levelFormat" /> -->
          <!-- <el-table-column label="付费类型" align="center" prop="payType" :formatter="payTypeFormat" /> -->
          <!-- <el-table-column label="付费价格" align="center" prop="payValue" /> -->
          <!-- <el-table-column label="折扣类型" align="center" prop="discountType" :formatter="discountTypeFormat" />
          <el-table-column label="折扣价格" align="center" prop="discountValue" />-->
          <!-- <el-table-column label="节目时长" align="center" width="100" prop="courseLength" /> -->
          
          <el-table-column
            label="节目状态"
            align="center"
            width="100px"
            prop="status"
            :formatter="statusFormat"
          />
          <!-- <el-table-column label="显示顺序" align="center" prop="orderNum" /> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <router-link
                :to="'/coursechapter/coursechapter/' + scope.row.courseId"
                class="link-type"
              >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-s-unfold"
                  v-hasPermi="['course:maintaincourse:edit']"
                >章节列表</el-button>
              </router-link>

              <!-- <el-table-column label="字典类型" align="center" :show-overflow-tooltip="true">
            <template slot-scope="scope"></template>
              </el-table-column>-->
              <router-link :to="'/maintaincourse/edit/' + scope.row.courseId" class="link-type">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  v-hasPermi="['course:maintaincourse:edit']"
                >修改</el-button>
              </router-link>

              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['course:maintaincourse:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改维修培训管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程标题" prop="courseTitle">
          <el-input v-model="form.courseTitle" placeholder="请输入课程标题" />
        </el-form-item>
        <el-form-item label="课程类型" prop="courseTypeId">
          <el-input v-model="form.courseTypeId" placeholder="请输入课程类型" />
        </el-form-item>
        <el-form-item label="浏览人数" prop="viewNum">
          <el-input v-model="form.viewNum" placeholder="请输入浏览人数" />
        </el-form-item>
        <el-form-item label="点赞人数" prop="likeNum">
          <el-input v-model="form.likeNum" placeholder="请输入点赞人数" />
        </el-form-item>
        <el-form-item label="课程缩略图路径" prop="courseThumbUrl">
          <el-input v-model="form.courseThumbUrl" placeholder="请输入课程缩略图路径" />
        </el-form-item>
        <el-form-item label="课程描述" prop="courseDes">
          <el-input v-model="form.courseDes" placeholder="请输入课程描述" />
        </el-form-item>
        <el-form-item label="课程内容" prop="courseContent">
          <el-input v-model="form.courseContent" placeholder="请输入课程内容" />
        </el-form-item>
        <el-form-item label="课程起始开始时间" prop="validStartTime">
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form.validStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择课程起始开始时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="课程起始结束时间" prop="validEndTime">
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form.validEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择课程起始结束时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="课程等级">
          <el-select v-model="form.level" placeholder="请选择课程等级">
            <el-option
              v-for="dict in levelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="购买类型 0免费 1付费">
          <el-select v-model="form.payType" placeholder="请选择购买类型 0免费 1付费">
            <el-option
              v-for="dict in payTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="购买价格" prop="payValue">
          <el-input v-model="form.payValue" placeholder="请输入购买价格" />
        </el-form-item>
        <el-form-item label="购买类型 0原价 1限时特价 2会员价">
          <el-select v-model="form.discountType" placeholder="请选择购买类型 0原价 1限时特价 2会员价">
            <el-option
              v-for="dict in discountTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="折扣价格" prop="discountValue">
          <el-input v-model="form.discountValue" placeholder="请输入折扣价格" />
        </el-form-item>
        <el-form-item label="会员购买价格" prop="payMemeberValue">
          <el-input v-model="form.payMemeberValue" placeholder="请输入会员购买价格" />
        </el-form-item>
        <el-form-item label="课程时长" prop="courseLength">
          <el-input v-model="form.courseLength" placeholder="请输入课程时长" />
        </el-form-item>
        <el-form-item label="是否有考试">
          <el-radio-group v-model="form.hasExam">
            <el-radio
              v-for="dict in hasExamOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="课程状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listCourse, getCourse, delCourse, addCourse, updateCourse, exportCourse } from "@/api/wechat/course";

import { treeselect } from "@/api/wechat/coursetype";



export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 维修培训管理表格数据
      maintaincourseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分类选项
      categoryOptions: undefined,
      // 课程等级字典
      levelOptions: [],
      // 购买类型 0免费 1付费字典
      payTypeOptions: [],
      // 购买类型 0原价 1限时特价 2会员价字典
      discountTypeOptions: [],
      // 是否有考试字典
      hasExamOptions: [],
      // 课程状态字典
      statusOptions: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseTitle: undefined,
        courseTypeId: 2000,
        viewNum: undefined,
        likeNum: undefined,
        courseThumbUrl: undefined,
        courseDes: undefined,
        courseContent: undefined,
        validStartTime: undefined,
        validEndTime: undefined,
        level: undefined,
        payType: undefined,
        payValue: undefined,
        discountType: undefined,
        discountValue: undefined,
        payMemeberValue: undefined,
        courseLength: undefined,
        hasExam: undefined,
        status: undefined,
        orderNum: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseTypeId: [
          { required: true, message: "课程类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("course_level").then(response => {
      this.levelOptions = response.data;
    });
    this.getDicts("pay_type").then(response => {
      this.payTypeOptions = response.data;
    });
    this.getDicts("discount_type").then(response => {
      this.discountTypeOptions = response.data;
    });
    this.getDicts("has_exam").then(response => {
      this.hasExamOptions = response.data;
    });
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询部门下拉树结构 */
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.courseTypeId = data.id;
      this.getList();
    },
    /** 查询维修培训管理列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.maintaincourseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 课程等级字典翻译
    levelFormat(row, column) {
      return this.selectDictLabel(this.levelOptions, row.level);
    },
    // 购买类型 0免费 1付费字典翻译
    payTypeFormat(row, column) {
      return this.selectDictLabel(this.payTypeOptions, row.payType);
    },
    // 购买类型 0原价 1限时特价 2会员价字典翻译
    discountTypeFormat(row, column) {
      return this.selectDictLabel(this.discountTypeOptions, row.discountType);
    },
    // 是否有考试字典翻译
    hasExamFormat(row, column) {
      return this.selectDictLabel(this.hasExamOptions, row.hasExam);
    },
    // 课程状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: undefined,
        courseTitle: undefined,
        courseTypeId: undefined,
        viewNum: undefined,
        likeNum: undefined,
        courseThumbUrl: undefined,
        courseDes: undefined,
        courseContent: undefined,
        validStartTime: undefined,
        validEndTime: undefined,
        level: undefined,
        payType: undefined,
        payValue: undefined,
        discountType: undefined,
        discountValue: undefined,
        payMemeberValue: undefined,
        courseLength: undefined,
        hasExam: "0",
        status: "0",
        orderNum: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加维修培训管理";
      // <router-link to="/user/profile">
      //       <el-dropdown-item>个人中心</el-dropdown-item>
      //     </router-link>
      this.$router.push({
        path: "/maintaincourse/edit",
        meta: { title: "aaa" }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      const courseId = row.courseId || this.ids;
      this.$router.push("/maintaincourse/edit/" + courseId);
      // getCourse(courseId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改维修培训管理";
      // });
    },
    handleChapter(row) {
      const courseId = row.courseId;
      this.$router.push({
        path: "/coursechapter/coursechapter",
        query: { courseId }
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != undefined) {
            updateCourse(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addCourse(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$confirm(
        '是否确认删除维修培训管理编号为"' + courseIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delCourse(courseIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有维修培训管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportCourse(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>